Page({
  data: {
    formats: {},
    bottom: 0,
    readOnly: false,
    placeholder: '开始输入...',
    _focus: false,
    navTop: 0,
    theme_id: -1,
    themeMessage: [],
    speech: false,
    src: ""
  },
  //预览
  priview() {
    this.editorCtx.getContents({
      success(res) {
        wx.setStorageSync("priviewHtml", res.html);
        wx.navigateTo({
          url: '/pages/add_detail/add_detail',
        });
      }
    })
  },
  onLoad(options) {
    let that = this;
    this.setData({
      navTop: getApp().globalData.navTop,
      heightConfig: getApp().globalData.windowHeight,
      themeMessage: wx.getStorageSync("themeMessage")
    })

    wx.showToast({
      title: '已开启自动保存',
      icon:"none"
    })

    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
      that.editorCtx.setContents({
        "html": wx.getStorageSync("priviewHtml")
      })
    }).exec()

    if (options.theme_id != null) {
      this.setData({
        theme_id: options.theme_id
      })
    }
  },
  formSubmit(e) {
    let that = this
    this.editorCtx.getContents({
      success(res) {
        if (res.html.length <= 13) {
          wx.showToast({
            title: '输入点东西吧',
            icon: 'none',
            image: '',
            duration: 1500,
            mask: false,
          });
          return
        }

        wx.showModal({
          title: '提示',
          content: '确定发布么？',
          showCancel: true,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: (result) => {

            if (result.confirm) {
              wx.showLoading({
                title: "发布中",
              });

              wx.request({
                url: getApp().globalData.url + '/saveArticle/',
                data: {
                  themeId: that.data.theme_id,
                  articleContent: res.html,
                },
                header: {
                  "authorization": wx.getStorageSync("token")
                },
                method: 'POST',
                success: (result) => {
                  wx.hideLoading();
                  if (result.data.code == 200) {
                    wx.redirectTo({
                      url: '/pages/index/index'
                    });
                    wx.setStorageSync("priviewHtml", "");
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: result.data.msg + '，错误码：' + result.data.code,
                      confirmText: '确定',
                      showCancel: false,
                    });
                  }
                },
              });
            }
          },
        })
      }
    })
  },
  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
    }).exec()
  },

  undo() {
    this.editorCtx.undo()
  },
  redo() {
    this.editorCtx.redo()
  },
  format(e) {
    let {
      name,
      value
    } = e.target.dataset
    if (!name) return
    // console.log('format', name, value)
    this.editorCtx.format(name, value)

  },
  onStatusChange(e) {
    const formats = e.detail
    this.setData({
      formats: formats
    })
  },
  insertDivider() {
    this.editorCtx.insertDivider({
      success: function () {
        console.log('insert divider success')
      }
    })
  },
  clear() {
    this.editorCtx.clear({
      success: function (res) {
        console.log("clear success")
      }
    })
  },
  removeFormat(e) {
    console.log(e)
    this.editorCtx.removeFormat()
  },
  insertDate() {
    const date = new Date()
    const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    this.editorCtx.insertText({
      text: formatDate
    })
  },
  input(){
    this.editorCtx.getContents({
      success(res) {
        if (res.html.length != 0) {
          wx.setStorageSync("priviewHtml", res.html);
        }

      }
    })
  },
})